package withdraw

import (
	"context"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/query"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type VirtualDicModel struct {
	logx.Logger
	ctx    context.Context
	tx     *query.Query
	svcCtx *svc.ServiceContext
}

func (model *VirtualDicModel) getDb() *gorm.DB {
	if model.svcCtx.Db != nil {
		return model.svcCtx.Db
	}

	return svc.Db
}
func NewVirtualDicModel(ctx context.Context, svc *svc.ServiceContext) *VirtualDicModel {
	return &VirtualDicModel{
		svcCtx: svc,
		ctx:    ctx,
		Logger: logx.WithContext(ctx),
	}
}
func (l *VirtualDicModel) GetRate(name string) (ret float64) {
	if l.tx == nil {
		l.tx = query.Use(l.getDb())
	}
	g := l.tx.VirtualDic
	d, err := g.Where(g.CoinName.Eq(name)).First()
	if err == nil {
		return d.ToRate
	}
	return
}
func (l *VirtualDicModel) GetRateList() []*rpc.VirtualDic {
	if l.tx == nil {
		l.tx = query.Use(l.getDb())
	}
	g := l.tx.VirtualDic
	d, err := g.Find()
	if err == nil {
		return d
	}
	return nil
}
